package com.jiang.jixiang.ui;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jiang.jixiang.databinding.ActivityAllhtmlBinding;
import com.just.agentweb.AgentWeb;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.utils.LogcatUtils;


/**
 * @创建者 ws
 * @创建时间 2021/9/14
 * @描述
 */
public class AllHtmlActivity extends BaseActivity {
    private ActivityAllhtmlBinding binding;
    private Bundle bundle;
    private String title;
    private String loadH5Url;
    private AgentWeb mAgentWeb;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityAllhtmlBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        bundle = getIntent().getExtras();
        title = bundle.getString("title");
        loadH5Url = bundle.getString("loadH5Url");
        LogcatUtils.e(" loadH5Url " + loadH5Url);
    }

    @Override
    protected void initView() {

        binding.inTtitle.titleTextTv.setText(title);
        binding.inTtitle.titleTextTv.setVisibility(View.VISIBLE);
        binding.inTtitle.titleLeftBtn.setOnClickListener(v -> {
            finish();
        });
        showProgressDialog(false);

        WebSettings webSettings = binding.webView.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //内容自适应
        //webSettings.setLoadWithOverviewMode(true);
        //webSettings.setUseWideViewPort(true);
        //设置支持缩放
        //webSettings.setBuiltInZoomControls(true);
        //webSettings.setDisplayZoomControls(false);
        //适配5.0不允许http和https混合使用情况
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

        //关闭缓存
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //关闭硬件加速 避免个别机型不支持报错
        webSettings.setBlockNetworkImage(false);
        //设置默认字体大小
        webSettings.setDefaultFontSize(16);


        String chromeUserAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36";
        // 设置修改后的 User Agent 字符串
        binding.webView.getSettings().setUserAgentString(chromeUserAgent);


        //加载需要显示的网页
        binding.webView.loadUrl(loadH5Url);
        binding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                stopProgressDialog();

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }
        });








        /*mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.webView, new FrameLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(loadH5Url);

        AgentWebConfig.clearDiskCache(this.getContext());*/


    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
